package com.luozongyao.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luozongyao.eduService.entity.EduCourse;
import com.luozongyao.eduService.entity.EduCourseDescription;
import com.luozongyao.eduService.entity.EduTeacher;
import com.luozongyao.eduService.entity.frontVo.CourseFrontVo;
import com.luozongyao.eduService.entity.frontVo.CourseWebVo;
import com.luozongyao.eduService.entity.vo.CourseInfoVo;
import com.luozongyao.eduService.entity.vo.CoursePublishVo;
import com.luozongyao.eduService.mapper.EduCourseMapper;
import com.luozongyao.eduService.service.EduChapterService;
import com.luozongyao.eduService.service.EduCourseDescriptionService;
import com.luozongyao.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozongyao.eduService.service.EduVideoService;
import com.luozongyao.servicebase.config.exceptionHandler.lzyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-11
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    //添加课程信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //1.向课程表添加信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert<=0){
            throw new lzyException(20001,"添加课程信息失败");
        }
        //获取添加之后的课程ID
        String cid = eduCourse.getId();
        //2.向课程简介表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程ID
        courseDescription.setId(cid);
        descriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2.查询描述表
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new lzyException(20001,"修改课程失败");
        }
        //2.修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublishById(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishById(courseId);
        return coursePublishVo;
    }

    @Override
    public void removeCourse(String courseId) {
        //1根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //3根据课程id删除章节描述 1vs1
        descriptionService.removeById(courseId);
        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if (result == 0 ){
            throw new lzyException(20001,"删除课程失败");
        }

    }

    //1条件查询带分页查询课程
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断值是否为空 不为空拼接条件
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){ //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){ //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){ //销售量
            wrapper.eq("buy_count",courseFrontVo.getBuyCountSort());
        }

        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //时间
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) { //价格
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords(); //总记录
        long pages = pageParam.getPages();
        long current = pageParam.getCurrent();
        long size = pageParam.getSize(); //一页多少条数
        long total = pageParam.getTotal(); // 总条数
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String,Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
